import Vue from 'vue'
import VueRouter from 'vue-router'
import Store from 'src/store'

import appRoutes from './appRoutes'
import { SessionStorage } from 'quasar'

Vue.use(VueRouter)
const store = new Store()
/*
 * If not building with SSR mode, you can
 * directly export the Router instantiation
 */

export default function (/* { store, ssrContext } */) {
  const Router = new VueRouter({
    scrollBehavior: () => ({ y: 0 }),
    routes: appRoutes,
    // Leave these as is and change from quasar.conf.js instead!
    // quasar.conf.js -> build -> vueRouterMode
    // quasar.conf.js -> build -> publicPath
    mode: process.env.VUE_ROUTER_MODE,
    base: process.env.VUE_ROUTER_BASE
  })
  // 全局前置守卫
  Router.beforeEach((to, from, next) => {
    console.log(Router.beforeEach)
    store.commit('router/change', from)
    if (to.matched.some(record => record.meta.requiresAuth)) {
      // 这里判断用户是否登录，验证本地存储是否有token
      if (!SessionStorage.getItem('token')) { // 判断当前的token是否存在
        next({
          path: '/account/login',
          query: { redirect: to.fullPath }
        })
      } else {
        console.log(to.path === '/pp/performance')
        if (to.path === '/pp/performance') {
          window.open('http://118.178.94.163:3000/login')
          next(false)
          return false
        } else if (to.path === '/pp/connectionpool') {
          window.open('http://118.178.94.163:80/systemDruid')
          next(false)
          return false
        } else {
          next()
        }
      }
    } else {
      next() // 确保一定要调用 next()
    }
    next()
  })
  return Router
}
